﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using MonoGame;
using Microsoft.Xna.Framework;

namespace TunnelsOfDeath.Managers
{
    class CameraManager : IEnumerable
    {

        protected List<ViewPosition> viewPositions = new List<ViewPosition>();

        public IEnumerator GetEnumerator()
        {
            foreach (ViewPosition item in viewPositions)
            {
                yield return item;
            }
        }

        public void Add(ViewPosition _toAdd)
        {
            viewPositions.Add(_toAdd);
            viewPositions.Sort();
        }

        public void RemoveNotUsed()
        {
            for (int i = viewPositions.Count; i >= 0; i--)
            {
                if (viewPositions[0].InUse == false)
                    viewPositions.RemoveAt(0);
            }
            viewPositions.Sort();
        }

        public void FindAssignPlayers()
        {
            ScreenSide side = ScreenSide.LEFT;
            viewPositions.Clear();
            foreach (Objects.Sprite item in Globals.Map)
            {
                if (item != null && item.Type == ObjectType.player)
                {
                    viewPositions.Add(new ViewPosition(item.position, side, (Objects.Player)item));
                    side++;
                }
            }
            viewPositions.Sort();
        }

        public bool SeparatorDisplay()
        {
            return (viewPositions.Count > 1 && viewPositions[0].slideAnimation != 0 && viewPositions[1].slideAnimation != 0);
        }

        public void UnregisterPlayer(Objects.Player _player)
        {
            foreach (ViewPosition item in viewPositions)
            {
                if (item.FollowedPlayer == _player)
                {
                    item.FollowedPlayer = null;
                }

            }
            if (viewPositions.Count > 1)
            {
                foreach (ViewPosition item in viewPositions)
                {
                    if (item.FollowedPlayer == null && item.Side == ScreenSide.RIGHT)
                    {
                        item.DesiredPositionX = Globals.ScreenX;
                        
                    }
                    if (item.FollowedPlayer == null && item.Side == ScreenSide.LEFT)
                    {
                        foreach (ViewPosition enlarge in viewPositions)
                        {
                            if (enlarge != item)
                            {
                                enlarge.DesiredPositionX = 0;
                                enlarge.Side = ScreenSide.LEFT;                             
                            }
                        }
                    }
                }
            }

        }

        public void CleanUp()
        {
            if (viewPositions.Count > 1)
            {
                if (viewPositions[0].InAnimation == false && viewPositions[1].InAnimation == false)
                {
                    for (int i = viewPositions.Count - 1; i >= 0; i--)
                    {
                        if (viewPositions[i].InAnimation == false && viewPositions[i].FollowedPlayer == null)
                            viewPositions.RemoveAt(i);
                    }
                }
            }
        }

        public ViewPosition this[int index]
        {
            get
            {
                return viewPositions[index];
            }
        }

    }
}
